<?php
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends CommonController
{
    public function index() {
        $map['a_id'] = I('get.id') ? I('get.id') : 0;
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = D('article');
        if ($map['a_id']) {
            $map['del'] = '0';
            $data = $MODEL->where($map)->page($page_no, $page_size)->order('orderby desc')->relation(true)->select();
            $count = $MODEL->where($map)->count();
        }else {
            $where['del'] = '0';
            $data = $MODEL->page($page_no, $page_size)->where($where)->order('orderby desc')->relation(true)->select();
            $count = $MODEL->where($where)->count();
        }
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();
        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('page', $show);
        $this->display();
    }

    public function add() {
        $data = M('article_classify')->where('state=1')->select();
        if (IS_POST) {
            $_POST['content']=htmlspecialchars($_POST['content']);
            $result = M('article')->add($_POST);
            if ($result) {
                $this->success('操作完成', 'Index');exit();
            }else{
                $this->error('数据未插入');
            }
        }
        $this->assign('data', $data);
        $this->display();
    }
    // 文章分类
    public function classify() {
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M('article_classify');
        $data = $MODEL->page($page_no, $page_size)->order('id')->select();
        
        $count = $MODEL->count();
        $Page = new \AntAge\Page($count, $page_size);
        $show = $Page->show();

        $this->assign('data', $data);
        $this->assign('count', $count);
        $this->assign('page', $show);
        $this->display();
    }

    public function del() {
        $id['id'] = $_GET['id'];
        $MODEL = M('article');
        $del['del']='1';
        $MODEL->where($id)->save($del);
        $this->success('删除完成', 'Index', 0);
    }
    public function save() {
        if (IS_POST) {
            $id = $_POST['id'];
            $_POST['content']=htmlspecialchars($_POST['content']);
            $result = M('article')->where('id='.$id)->save($_POST);
            if ($result === false) {
                $this->error('更新失败');
            }else{
                $this->success('更新成功','index');
            }
        }else{
            $MODEL = M('article_classify');
            $list = $MODEL->where('state=1')->select();
            
            $id = $_GET['id'];
            $MODEL = D('Article');
            $data = $MODEL->relation(true)->find($id);
            $data['content'] = htmlspecialchars_decode($data['content']);
            $this->assign('list', $list);
            $this->assign('data', $data);
            $this->display();
        }
    }
    public function classify_save() {
        $id = $_GET['id'];
        $MODEL = M('article_classify');
        $data = $MODEL->find($id);
        $this->assign('data', $data);
        $this->display();
    }

    public function updata() {
        $image_arr = com_upload();
        $MODEL = M('article');
        $MODEL->create();
        if($image_arr[img]) {$MODEL->img = $image_arr[img];}
        $MODEL->save();
        $this->success('修改完成', 'Index', 0);
    }

    public function classify_updata() {
        $MODEL = M('article_classify');
        $MODEL->create();
        $MODEL->save();
        $this->success('操作完成', 'Classify', 0);
    }
    public function classify_add() {
    $MODEL = M('article_classify');
    if ($MODEL->create()) {
        $result = $MODEL->add();
        if ($result) {
            $this->success('操作完成', 'Classify', 0);exit();
        }
        else{
            $this->error('数据未插入');
        }
    }
    $this->display();
    }
    public function classify_del() {
        $id = $_GET['id'];
        $MODEL = M('article_classify');
        $MODEL->delete($id);
        $this->success('操作完成', 'Classify', 0);
    }
}
